Show the code
import pandas as pd
import numpy as np
from lets_plot import *
LetsPlot.setup_html(isolated_frame=True)import pandas as pd
import numpy as np
from lets_plot import *
LetsPlot.setup_html(isolated_frame=True)# Learn morea about Code Cells: https://quarto.org/docs/reference/cells/cells-jupyter.html
# Include and execute your code here
from palmerpenguins import load_penguins
df = load_penguins()Include the tables created from PY4DS: CH2 Data Visualization used to create the above chart (Hint: copy the code from 2.2.1. The penguins data frame and paste each in the cells below)
penguins = load_penguins()
penguins| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | |
|---|---|---|---|---|---|---|---|---|
| 0 | Adelie | Torgersen | 39.1 | 18.7 | 181.0 | 3750.0 | male | 2007 |
| 1 | Adelie | Torgersen | 39.5 | 17.4 | 186.0 | 3800.0 | female | 2007 |
| 2 | Adelie | Torgersen | 40.3 | 18.0 | 195.0 | 3250.0 | female | 2007 |
| 3 | Adelie | Torgersen | NaN | NaN | NaN | NaN | NaN | 2007 |
| 4 | Adelie | Torgersen | 36.7 | 19.3 | 193.0 | 3450.0 | female | 2007 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 339 | Chinstrap | Dream | 55.8 | 19.8 | 207.0 | 4000.0 | male | 2009 |
| 340 | Chinstrap | Dream | 43.5 | 18.1 | 202.0 | 3400.0 | female | 2009 |
| 341 | Chinstrap | Dream | 49.6 | 18.2 | 193.0 | 3775.0 | male | 2009 |
| 342 | Chinstrap | Dream | 50.8 | 19.0 | 210.0 | 4100.0 | male | 2009 |
| 343 | Chinstrap | Dream | 50.2 | 18.7 | 198.0 | 3775.0 | female | 2009 |
344 rows × 8 columns
include figures in chunks and discuss your findings in the figure.
# Include and execute your code here
penguins.head()| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | |
|---|---|---|---|---|---|---|---|---|
| 0 | Adelie | Torgersen | 39.1 | 18.7 | 181.0 | 3750.0 | male | 2007 |
| 1 | Adelie | Torgersen | 39.5 | 17.4 | 186.0 | 3800.0 | female | 2007 |
| 2 | Adelie | Torgersen | 40.3 | 18.0 | 195.0 | 3250.0 | female | 2007 |
| 3 | Adelie | Torgersen | NaN | NaN | NaN | NaN | NaN | 2007 |
| 4 | Adelie | Torgersen | 36.7 | 19.3 | 193.0 | 3450.0 | female | 2007 |
Recreate the example charts from PY4DS: CH2 Data Visualization of the textbook. (Hint: copy the chart code from 2.2.3. Creating a Plot, one for each cell below)
# Include and execute your code here
(
ggplot(data=penguins, mapping=aes(x="flipper_length_mm", y="body_mass_g"))
+ geom_point()
)This is a scatter plot that visualizes the relationship between a study sample of penguins’ flipper length in millimeters and body mass. Generally we see that as the flipper length increases so does the body mass.
# Include and execute your code here
(
ggplot(
data=penguins,
mapping=aes(x="flipper_length_mm", y="body_mass_g", color="species"),
)
+ geom_point()
)In this scatter plot we have specified the individual species included in the study with the legend on the right, and separated the species by color. We see that the Gentoo penguins are generally the largest with the highest body mass and longest flippers.
# Include and execute your code here
(
ggplot(
data=penguins,
mapping=aes(x="flipper_length_mm", y="body_mass_g", color="species"),
)
+ geom_point()
+ geom_smooth(method="lm")
)In this plot we have added a line of best fit that maps the progression of values using a linear model. The species each have their own line of best fit, with the species legend indicating which line corresponds to which species.
# Include and execute your code here
(
ggplot(data=penguins, mapping=aes(x="flipper_length_mm", y="body_mass_g"))
+ geom_point(mapping=aes(color="species"))
+ geom_smooth(method="lm")
)In this chart we have combined the three lines of best fit into one linear regression that corresponds to the progression of values of all three species together.
# Include and execute your code here
(
ggplot(data=penguins, mapping=aes(x="flipper_length_mm", y="body_mass_g"))
+ geom_point(aes(color="species", shape="species"))
+ geom_smooth(method="lm")
+ labs(
title="Body mass and flipper length",
subtitle="Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x="Flipper length (mm)",
y="Body mass (g)",
color="Species",
shape="Species",
)
)In this final chart we have separated the points corresponding to each species by shape, included the line of best fit from the previous chart. We’ve cleaned up the axis titles, species legend, and added the title for the overall visualization of the data.